/* Display.h
Copyright (c) 2008 Michael Zahniser
Please note the license terms (MIT license) at the end of this document.

Header file for the Display class, which handles the Mirth window.
*/

#ifndef __MZ_DISPLAY_H__
#define __MZ_DISPLAY_H__

#include "_sdl/Window.h"
#include "_sdl/Surface.h"
#include "_sdl/Font.h"
#include "_web/Curl.h"
#include "Scraper.h"

#include <vector>

using std::vector;



class Display {
public:
	// Constructor. Takes the path to the directory which contains the program resources
	// (such as the font image and the preferences file).
	Display(const string &resourcePath);
	~Display();
	
	// Run the event loop.
	void Run();
	
	
private:
	// Initialize the display.
	void Initialize();
	// Initialize the array of web scraper objects.
	void MakeScrapers();
	// Save the preferences (current date for each comic).
	// Eventually this will also include whether or not to view each comic.
	void SavePreferences();
	// Load whatever image is in the buffer.
	void LoadImage();
	// Create the window, big enough to include the current image, with the given
	// number of additional pixels space on the bottom for the status bar.
	void MakeWindow(int statusHeight = 16);
	// Draw the window.
	void Draw();
	void DrawPreferences();
	// Handle the given key. The return value is a Scraper pointing to the image
	// that the key takes you to.
	Scraper *DoKey(SDLKey key, vector<Scraper *>::iterator &it);

	// This function filters out all events except key presses and window closing.
	static int EventFilter(const SDL_Event *event);


private:
	// Flag to store whether the constructor was successful in loading all resources.
	bool isInitialized;
	// Path to the resource directory.
	string resourcePath;
	
	// The window.
	Window window;
	// The currently loaded image.
	Surface image;
	// Object for drawing text.
	Font font;
	// Buffer to store the image in.
	string imageBuffer;
	
	// Array of web scrapers, and iterators to the scraper corresponding to the image being
	// displayed, and to the next image that will be displayed (assuming the user presses
	// the same key that was pressed last).
	vector<Scraper *> scrapers;
	vector<Scraper *>::iterator scraper;
	vector<Scraper *>::iterator next;
	vector<bool> active;
	// Most recent key press. If the user presses the same key repeatedly, the image for the
	// next press of that key is preemptively loaded while the user is reading each comic.
	SDLKey previousKey;
	// This Scraper object will be used if the program guessed the next key press correctly.
	Scraper *nextScraper;
	// This flag is set if a key press results in a new image being loaded. The next image
	// is only preemptively loaded when the comic has changed. (i.e. if the user just presses
	// <space> to scroll, don't re-load the next comic.)
	bool comicChanged;

	// Object representing an internet connection through libcurl.
	Curl curl;
	
	// Vertical scroll, in pixels.
	int scroll;
	
	// Show the preferences page.
	bool showPreferences;
	int preferenceScroll;
	int preferenceSelect;


private:
	// Pressing the space bar scrolls by this number of pixels.
	static const int SCROLL_AMOUNT = 200;
	// Limit the window to this height.
	static const int MAXIMUM_WINDOW_HEIGHT = 600;
};



#endif

/* Copyright (c) 2008 Michael Zahniser

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE. */
